package com.yanqu.road.dao.impl.channelcenter.washe.peak;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.jinbiao.WasheJinBiaoPeakBattleData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheJinBiaoPeakBattleDaoImpl extends TempDataObjectDao<WasheJinBiaoPeakBattleData> {

    public static String getWasheJinBiaoPeakBattleDataMapSql = "select * from `t_n_washe_jinbiao_peak_battle` where `seasonId`=?";

    public Map<Integer, List<WasheJinBiaoPeakBattleData>> getWasheJinBiaoPeakBattleDataMap(int seasonId) {
        Map<Integer, List<WasheJinBiaoPeakBattleData>> map = new ConcurrentHashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getWasheJinBiaoPeakBattleDataMapSql);
            int i = 1;
            call.setInt(i++, seasonId);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheJinBiaoPeakBattleData data = parse(rs);
                List<WasheJinBiaoPeakBattleData> peakDataList = map.getOrDefault(data.getRound(), new ArrayList<>());
                peakDataList.add(data);
                map.put(data.getRound(), peakDataList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return map;
    }

    public static String updateWasheJinBiaoPeakDataSql = "update `t_n_washe_jinbiao_peak_battle` set score1=?,score2=?,battleResult=? where seasonId=? and round=? and serverId1=? and userId1=? and serverId2=? and userId2 =?";

    @Override
    protected boolean updateData(WasheJinBiaoPeakBattleData peakBattleData) {
        peakBattleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getScore1()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getScore2()));
        para.put(i++, new DbParameter(Types.INTEGER, peakBattleData.getBattleResult()));
        para.put(i++, new DbParameter(Types.INTEGER, peakBattleData.getSeasonId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakBattleData.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getServerId1()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getUserId1()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getServerId2()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getUserId2()));
        if (getDb().executeNoneQuery(updateWasheJinBiaoPeakDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLuckyDrawUserDataSql = "insert into `t_n_washe_jinbiao_peak_battle`(seasonId,round,serverId1,userId1,score1,serverId2,userId2,score2,battleResult) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(WasheJinBiaoPeakBattleData peakBattleData) {
        peakBattleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakBattleData.getSeasonId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakBattleData.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getServerId1()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getUserId1()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getScore1()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getServerId2()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getUserId2()));
        para.put(i++, new DbParameter(Types.BIGINT, peakBattleData.getScore2()));
        para.put(i++, new DbParameter(Types.INTEGER, peakBattleData.getBattleResult()));
        if (getDb().executeNoneQuery(insertLuckyDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected WasheJinBiaoPeakBattleData parseData(ResultSet rs) throws SQLException {
        WasheJinBiaoPeakBattleData battleData = new WasheJinBiaoPeakBattleData();
        battleData.setSeasonId(rs.getInt("seasonId"));
        battleData.setRound(rs.getInt("round"));
        battleData.setServerId1(rs.getLong("serverId1"));
        battleData.setUserId1(rs.getLong("userId1"));
        battleData.setScore1(rs.getInt("score1"));
        battleData.setServerId2(rs.getLong("serverId2"));
        battleData.setUserId2(rs.getLong("userId2"));
        battleData.setScore2(rs.getInt("score2"));
        battleData.setBattleResult(rs.getInt("battleResult"));
        return battleData;
    }

}
